package com.miraclengine.karma;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.texturepacker.opengl.texture.util.texturepacker.TexturePackTextureRegionLibrary;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.debug.Debug;
import org.andengine.util.modifier.IModifier;

import com.miraclengine.texture.TextureData;

public class LogoScene extends AbstractScene {
	
	private String TAG = LogoScene.class.getSimpleName();

	private KarmaGame game;
	
	//TextureRegion
	TextureRegion logoBgRegion;
	TextureRegion logoWingRegion;
	
	//Sprite
	private Sprite logoBg;
	private Sprite logoWing;
	
	private TexturePackTextureRegionLibrary packer;
	
	private SequenceEntityModifier sequenceModifier;
	private AlphaModifier alphaModifier;
	
	public LogoScene(KarmaGame karmaGame) {
		// TODO Auto-generated constructor stub
		this.game = karmaGame;
		this.packer = karmaGame.mSpritesheetTexturePackTextureRegionLibrary;
		
		loadResources();
	}
	
	public void loadResources() {
		this.logoBgRegion = this.packer.get(TextureData.LOGO_BACK_ID);
		this.logoWingRegion = this.packer.get(TextureData.LOGO_WING_ID);
	}
	
	@Override
	public void drawScene() {
		// TODO Auto-generated method stub
		this.setBackground(new Background(0.f, 0.f, 0.f));
		
		this.logoBg = new Sprite(0, 0, this.logoBgRegion, game.getVertexBufferObjectManager());
		this.logoWing = new Sprite(347, 200, this.logoWingRegion, game.getVertexBufferObjectManager());
		
		this.attachChild(logoBg);
		this.attachChild(logoWing);
		
		final DelayModifier delayModifer = new DelayModifier(20.f, 
				new IEntityModifierListener() {
					@Override
					public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
					}
		
					@Override
					public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
						LogoScene.this.logoWing.registerEntityModifier(LogoScene.this.sequenceModifier);
						if(Settings.soundEnabled) {
							LogoScene.this.game.mLogoSound.play();
						}
					}
				}
		);
		
		sequenceModifier = new SequenceEntityModifier(
				new IEntityModifierListener() {

					@Override
					public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
						LogoScene.this.logoBg.registerEntityModifier(alphaModifier);
						//LogoScene.this.logoWing.registerEntityModifier(alphaModifier.deepCopy());
						LogoScene.this.detachChild(logoWing);
					}
				},
				new ScaleModifier(0.1f, 1, 1.15f),
				new ScaleModifier(0.1f, 1.15f, 1),
				new ScaleModifier(0.1f, 1, 1.07f),
				new ScaleModifier(0.1f, 1.07f, 1),
				new DelayModifier(2.0f)
		);
		
		alphaModifier = new AlphaModifier(2.f, 1.f, 0.f, new IEntityModifierListener() {

			@Override
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
				// TODO Auto-generated method stub
				Debug.d(TAG, "alpha start!");
			}

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
				// TODO Auto-generated method stub
				Debug.d(TAG, "alpha finish!");
				
				if(isFirst) {
					LogoScene.this.game.goTitle();
				}
			}
			
		});
		
		this.logoWing.registerEntityModifier(delayModifer);
	}
	
	boolean isFirst = true;

}
